<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生列表 - 学生管理系统</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/style.css">
</head>
<body>
<div class="dashboard">
    <header class="header">
        <h1>学生管理系统</h1>
        <div class="user-info">
            欢迎, ${sessionScope.user.username} | <a href="${pageContext.request.contextPath}/logout">退出</a>
        </div>
    </header>

    <nav class="sidebar">
        <ul>
            <li><a href="${pageContext.request.contextPath}/student" class="active">学生管理</a></li>
            <li><a href="${pageContext.request.contextPath}/course">课程管理</a></li>
            <li><a href="${pageContext.request.contextPath}/enrollment">选课管理</a></li>
            <li><a href="${pageContext.request.contextPath}/dashboard">返回控制面板</a></li>
        </ul>
    </nav>

    <main class="content">
        <h2>学生列表</h2>

        <div class="actions">
            <a href="${pageContext.request.contextPath}/student/add" class="btn">添加学生</a>
        </div>

        <div class="pagination-controls">
            <form action="${pageContext.request.contextPath}/student" method="get">
                <div class="form-group">
                    <label for="pageSize">每页显示:</label>
                    <select id="pageSize" name="pageSize" onchange="this.form.submit()">
                        <option value="5" ${pageBean.pageSize == 5 ? 'selected' : ''}>5</option>
                        <option value="10" ${pageBean.pageSize == 10 ? 'selected' : ''}>10</option>
                        <option value="20" ${pageBean.pageSize == 20 ? 'selected' : ''}>20</option>
                        <option value="50" ${pageBean.pageSize == 50 ? 'selected' : ''}>50</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="sortField">排序字段:</label>
                    <select id="sortField" name="sortField">
                        <option value="studentId" ${'studentId' eq pageBean.sortField ? 'selected' : ''}>学号</option>
                        <option value="name" ${'name' eq pageBean.sortField ? 'selected' : ''}>姓名</option>
                        <option value="gender" ${'gender' eq pageBean.sortField ? 'selected' : ''}>性别</option>
                        <option value="major" ${'major' eq pageBean.sortField ? 'selected' : ''}>专业</option>
                        <option value="status" ${'status' eq pageBean.sortField ? 'selected' : ''}>状态</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="sortOrder">排序方式:</label>
                    <select id="sortOrder" name="sortOrder">
                        <option value="ASC" ${'ASC' eq pageBean.sortOrder ? 'selected' : ''}>升序</option>
                        <option value="DESC" ${'DESC' eq pageBean.sortOrder ? 'selected' : ''}>降序</option>
                    </select>
                </div>

                <button type="submit" class="btn">应用</button>
            </form>
        </div>

        <table class="data-table">
            <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>出生日期</th>
                <th>专业</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:choose>
                <c:when test="${not empty pageBean.list}">
                    <c:forEach items="${pageBean.list}" var="student">
                        <tr>
                            <td>${student.studentId}</td>
                            <td>${student.name}</td>
                            <td>${student.gender}</td>
                            <td><fmt:formatDate value="${student.birthDate}" pattern="yyyy-MM-dd" /></td>
                            <td>${student.major}</td>
                            <td>${student.status}</td>
                            <td>
                                <a href="${pageContext.request.contextPath}/student/edit?id=${student.studentId}" class="btn-small">编辑</a>
                                <a href="${pageContext.request.contextPath}/student/delete?id=${student.studentId}" class="btn-small btn-danger" onclick="return confirm('确定要删除该学生吗？')">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                </c:when>
                <c:otherwise>
                    <tr>
                        <td colspan="7">没有找到学生记录</td>
                    </tr>
                </c:otherwise>
            </c:choose>
            </tbody>
        </table>

        <div class="pagination">
            <c:if test="${pageBean.hasPreviousPage}">
                <a href="${pageContext.request.contextPath}/student?pageNum=${pageBean.pageNum - 1}&pageSize=${pageBean.pageSize}&sortField=${pageBean.sortField}&sortOrder=${pageBean.sortOrder}" class="btn-small">上一页</a>
            </c:if>

            <span class="page-info">
                    第 ${pageBean.pageNum} 页，共 ${pageBean.totalPage} 页，总记录数 ${pageBean.totalRecord}
                </span>

            <c:if test="${pageBean.hasNextPage}">
                <a href="${pageContext.request.contextPath}/student?pageNum=${pageBean.pageNum + 1}&pageSize=${pageBean.pageSize}&sortField=${pageBean.sortField}&sortOrder=${pageBean.sortOrder}" class="btn-small">下一页</a>
            </c:if>
        </div>
    </main>

    <footer class="footer">
        <p>&copy; 2023 学生管理系统. All rights reserved.</p>
    </footer>
</div>
</body>
</html>